﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Maticsoft.Web
{
    public partial class Addr : PageBaseMember
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //AjaxProcess();
            if (!IsPostBack)
            {
                InitData();
            }
        }

        protected void InitData()
        {
            BindProvince();
            BindCity(0);
        }

        protected void BindProvince()
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("locationId<100");
            ddlProvince.DataSource = list;
            ddlProvince.DataTextField = "locationName";
            ddlProvince.DataValueField = "locationId";
            ddlProvince.DataBind();

            ddlProvince.Items.Insert(0, new ListItem("选择省", "-1"));
        }

        protected void BindCity(int provinceId)
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("pId=" + provinceId + " and locationId=cId");
            ddlCity.DataSource = list;
            ddlCity.DataTextField = "locationName";
            ddlCity.DataValueField = "locationId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("选择市", "-1"));
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCity(int.Parse(ddlProvince.SelectedValue));
        }

        protected void AjaxProcess()
        {
            //if (Request.QueryString["sync"] == "true")
            //{
            //    BLL.DeliveryAddr addrBLL = new BLL.DeliveryAddr();
            //    Model.DeliveryAddr addrModel = new Model.DeliveryAddr();
            //    addrModel.Name = Request.QueryString["name"];
            //    addrModel.Mobile = Request.QueryString["mobile"];
            //    addrModel.PostCode = Request.QueryString["code"];
            //    addrModel.Address = Request.QueryString["addr"];
            //    int uid = 0;
            //    int.TryParse(this.UserId, out uid);
            //    addrModel.UserId = uid;
            //    addrModel.CreateTime = DateTime.Now;
            //    addrModel.UpdateTime = DateTime.Now;
            //    addrBLL.Add(addrModel);

            //    Response.Write("1");
            //    Response.End();
            //}
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.DeliveryAddr addrBLL = new BLL.DeliveryAddr();
            if (cbMain.Checked)
            {
                List<Model.DeliveryAddr> list = addrBLL.GetModelList("UserId=" + this.UserId + " and IsMain=1 and IsActive=1");
                if (list.Count > 0)
                {
                    list[0].IsMain = 0;
                    addrBLL.Update(list[0]);
                }
            }
            Model.DeliveryAddr addrModel = new Model.DeliveryAddr();
            addrModel.Name = txtName.Value;
            addrModel.Mobile = txtMobile.Value;
            addrModel.PostCode = txtPostCode.Value;
            addrModel.Address = txtAddress.Value;
            addrModel.IdentityNo = txtIndentityNo.Value;
            addrModel.LocationId = DigitHelper.GetInt32(ddlCity.SelectedValue);
            ImageUpload img = new ImageUpload();
            if (fileFront.Value != "")
            {
                img.SavePath = "~/uploadpic/identity/";
                img.FormFile = fileFront;
                img.Upload();
                if (img.Error == 0)
                {
                    addrModel.IdentityFrontPic = img.OutFileName;
                }
            }
            if (fileBack.Value != "")
            {
                img.SavePath = "~/uploadpic/identity/";
                img.FormFile = fileBack;
                img.Upload();
                if (img.Error == 0)
                {
                    addrModel.IdentityBackPic = img.OutFileName;
                }
            }
            int uid = 0;
            int.TryParse(this.UserId, out uid);
            addrModel.UserId = uid;
            addrModel.CreateTime = DateTime.Now;
            addrModel.UpdateTime = DateTime.Now;
            addrModel.IsMain = cbMain.Checked ? 1 : 0;
            addrModel.IsActive = 1;
            addrBLL.Add(addrModel);


            Response.Write("<script>parent.window.location.href = \"Settlement.aspx?ids=" + Request.QueryString["ids"] + "\";</script>");
        }
    }
}